import django.forms as forms

from django_widgets.forms import DivForm,  FormValidator,  RecaptchaForm

import tags.helpers as tag_helpers
import community.helpers as community_helpers
import django_widgets.widgets as form_widgets
import django_widgets.html_helpers as html_helpers
import django_qa.models as models

from django_qa.models import Question

class QuestionForm(RecaptchaForm):
    title = forms.CharField(max_length = 70,  min_length = 3,  required = True,  label = 'Title',  
                           widget = form_widgets.StandardCharfield(attrs={'class':'required',  'minlength':'3'}))
    question = forms.CharField(max_length = 500,  
                                min_length = 5, 
                                widget = form_widgets.GiantTextarea(attrs={'class':'required',  'minlength':'5'}), 
                                required = True, 
                                label = 'Question', 
                                help_text = 'What is your question?. Be descriptive. Required.')
    tags = forms.CharField(max_length = 200, 
                                min_length = 5, 
                                required = True, 
                                widget = form_widgets.StandardCharfield(attrs={'class':'required',  'minlength':'7'}), 
                                help_text = 'Combine multiple words into single-words. Seperate tags using commas. Maximum five tags. At least one tag required.')
    captcha = forms.CharField(label = 'Are you human?', widget = form_widgets.RecaptchaField(),  required = False)
    
    def handle_add(self,  request):
        if self.data:
            user = community_helpers.get_logged_user(request)
            
            name = self.data['title']
            question = html_helpers.strip_all_tags(self.data['question'])
            tags = self.data['tags']
            
            new_question = models.Question(name = name,  descriptor = question,  user = user)
            new_question.log_save(user)
            
            cleaned_tags = tag_helpers.parse_tags(tags,  ',')
            for tag in cleaned_tags:
                new_question.add_tag(tag,  user)
            return new_question
        else:
            return None

class AnswerForm(DivForm):
    answer = forms.CharField(max_length = 2000,  
                            min_length = 5, 
                            widget = form_widgets.BigTextarea(attrs={'class':'required',  'minlength':'5'}), 
                            required = True, 
                            label = 'Your Answer')
    
    def handle_add(self,  request,  question):
        if self.data:
            user = community_helpers.get_logged_user(request)
            
            if user:
                answer = html_helpers.strip_all_tags(self.data['answer'])
                question.add_answer(answer,  user)
                
def build_edit_question_form(question):
    base_fields = {'title':  forms.CharField(max_length = 70,  min_length = 3,  required = True,  label = 'Title',  
                            initial = question.name, 
                           widget = form_widgets.StandardCharfield(attrs={'class':'required',  'minlength':'3'})) , 
                            'question': forms.CharField(max_length = 500,  
                                min_length = 5, 
                                initial = question.descriptor, 
                                widget = form_widgets.GiantTextarea(attrs={'class':'required',  'minlength':'5'}), 
                                required = True, 
                                label = '', 
                                help_text = 'What is this question?. Be descriptive. Required.'), 
                           }
                           
    def handle_edit(self,  user,  object):
        if self.data:
            object.log_edit(user,self.cleaned_data['title'],  html_helpers.strip_all_tags(self.cleaned_data['question']))
            
    EditQuestionForm = type('EditQuestionForm',  (DivForm, ),  base_fields)
    EditQuestionForm.handle_edit = handle_edit
    return EditQuestionForm

def build_edit_answer_form(answer):
    base_fields = {'answer': forms.CharField(max_length = 500,  
                                min_length = 5, 
                                initial = answer.descriptor, 
                                widget = form_widgets.GiantTextarea(attrs={'class':'required',  'minlength':'5'}), 
                                required = True, 
                                label = '', 
                                help_text = 'What is your answer?. Be descriptive. Required.'), 
                           }
                           
    def handle_edit(self,  user,  object):
        if self.data:
            object.log_edit(user, html_helpers.strip_all_tags(self.cleaned_data['answer']))

    EditAnswerForm = type('EditAnswerForm',  (DivForm, ),  base_fields)
    EditAnswerForm.handle_edit = handle_edit
    return EditAnswerForm
